import { _decorator, AudioSource, Component, director, Node } from 'cc';
import { ResManager } from '../res/ResManager';
const { ccclass, property } = _decorator;

@ccclass('AudioManager')
export class AudioManager {
    private static _instance: AudioManager = null;

    private m_AudioNode: Node = null;
    private m_AudioSource: AudioSource = null;

    static getInstance() {
        if (this._instance === null) {
            this._instance = new AudioManager();
        }
        return this._instance;
    }

    private constructor() {
        this.m_AudioNode = director.getScene().getChildByName("Canvas");
        this.m_AudioSource = this.m_AudioNode.addComponent(AudioSource);
    }
    /**
     * 
     * @param bUrl 
     */
    playMusic(bUrl: {
        b: string,/**子包名*/
        u: string,/**资源路径*/
    }): void {
        ResManager.getInstance().loadAudioClip(bUrl.b, bUrl.u, audioClip => {
            let audioSource = this.m_AudioSource;
            audioSource.clip = audioClip;
            audioSource.loop = true;
            audioSource.play();
        })
    }
}


